#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   ftp_dir.sh
# Version:    1.0
# Date:       2013/08/20
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    02系统基本功能-04网络服务-01vsftp-01字符模式-05目录选项
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/06/24
#             - Allow anonymous login, and ls .file, with local time
# ----------------------------------------------------------------------
##! @TODO:  update vsftpd.conf, and restart vsftpd
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_setup()
{
    serv=vsftpd
    RHOST=${RHOST}

    TEST_USER="anonymous"
    TEST_USER_PASSWD="noone@nowhere.com"
    VSFTP_CONF="/etc/vsftpd/vsftpd.conf"

    exists expect ftp vsftpd

    # create tmp file , for upload
    TMP_DIR="/tmp/vsftp$$"
    mkdir -p $TMP_DIR
    EchoResult "mkdir $TMP_DIR"

    echo "test" > $TMP_DIR/.testfile

    ${RSH} -l root -n $RHOST "cp $VSFTP_CONF $VSFTP_CONF.bak"
    TMP_VSFTP_CONF="/tmp/vsftpd.conf" 
    cat > ${TMP_VSFTP_CONF} <<-EOF
anonymous_enable=YES

local_enable=YES

write_enable=YES

local_umask=022

dirmessage_enable=YES

xferlog_enable=YES

connect_from_port_20=YES

xferlog_std_format=YES

chroot_local_user=NO

listen=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

dirlist_enable=YES
use_localtime=YES
force_dot_files=YES
anon_upload_enable=YES
EOF

    ${RCP} $TMP_VSFTP_CONF root@$RHOST:$VSFTP_CONF
    # restart 
    ${RSH} -l root $RHOST \
       "command -v systemctl && /bin/systemctl restart ${serv}.service || /sbin/service ${serv} restart"

    RE=`${RSH} -l root $RHOST \
       "/bin/netstat -ntlp | grep ${serv}"`
    echo "${RE}" | grep ${serv}
    EchoResult "service ${serv} restart test."

    # upload upfile to server:/var/ftp/ftpdir
    UP_DIR="/var/ftp/ftpdir"
    ${RSH} -l root -n $RHOST "mkdir $UP_DIR; [ -d $UP_DIR ] && chmod 777 $UP_DIR || echo \"$UP_DIR not exits.\""

    trap do_cleanup INT EXIT

}

##! @TODO:  upload  .file, ls file with local time.
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_test()
{
    echo "Test: anonymous login, upload and ls .file, with local time."    
    LOCAL_TIME=`${RSH} -n $RHOST 'LANG=C date +%b\ %d\ %R\ .testfile'`
    echo $LOCAL_TIME
    expect -c "
        spawn ftp $RHOST
        expect -re \": \"
        send \"$TEST_USER\r\"
        expect -re \"Password:\"
        send \"$TEST_USER_PASSWD\r\"

            expect {
                # 530 - Login failed
                \"530\" {send_user \"==> TEST : FAIL (ftp rejected login attempt)\n\";exit 1}
                # 230 - Login successful
                \"230\" {send_user \"==> TEST : PASS (ftp allowed login attempt)\n\"}
            }

       expect \"ftp> \"
       send \"lcd $TMP_DIR\r\"
        expect { 
            # 550 - failed
            \"550\" { send_user \"==> TEST : FAIL anonymous put file.\n\";exit 1 }
            # 226 - transfer successful
            \"226\" {send_user \"==> TEST : PASS anonymous put file.\n\"}
        }
       expect \"ftp> \"
       send \"cd ftpdir\r\"
        expect { 
            # 550 - failed
            \"550\" { send_user \"==> TEST : FAIL anonymous put file.\n\n\";exit 1 }
            # 226 - transfer successful
            \"226\" {send_user \"==> TEST : PASS anonymous put file.\n\"}
        }
       expect \"ftp> \"
       send \"put .testfile\r\" 
        expect { 
            # 550 - failed
            \"550\" { send_user \"==> TEST : FAIL anonymous put file.\n\";exit 1 }
            # 553 - failed
            \"553\" { send_user \"==> TEST : FAIL anonymous put file.\n\";exit 1 }
            # 226 - transfer successful
            \"226\" {send_user \"==> TEST : PASS anonymous put file.\n\"}
        }


       expect \"ftp> \"
       send \"ls\r\"       
       expect { 
             # 550 - failed
             \"550\" { send_user \"==> TEST : FAIL anonymous ls .file.\n\";exit 1 }
             # .testfile - ls
             \".testfile\" { send_user \"==> TEST : PASS anonymous ls .file.\n\"}
              }

       # test file with local time
       expect \"ftp> \"
       send \"ls .testfile\r\"       
       expect { 
             # 550 - failed
             \"550\" { send_user \"==> TEST : FAIL anonymous ls .file with local time.\n\";exit 1 }
             # time
             -re \"$LOCAL_TIME\" { send_user \"==> TEST : PASS anonymous ls .file with local time.\n\"}
              }


       expect \"ftp> \"
       send \"bye\r\"

       expect eof

       catch wait result
       exit [lindex \$result 3]

    "
}

##! @TODO:  cleanup tmp files, back up vsftpd.conf and restart ftp services.
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure
function do_cleanup()
{
    ${RSH} -l root -n $RHOST "[ -f $VSFTP_CONF.bak ] && mv -f $VSFTP_CONF.bak $VSFTP_CONF"
    ${RSH} -l root -n $RHOST "command -v systemctl && /bin/systemctl stop ${serv}.service || /sbin/service ${serv} stop"
    ${RSH} -l root -n $RHOST "rm -rf $UP_DIR"

    rm -rf $TMP_DIR $TMP_VSFTP_CONF

}
# include lib files

if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh
. ${SFROOT}/lib/XmlParse.sh

. ${CMDLIB}/setup.sh

# get RHOST, RUSERNAME, PASSWORD from config.xml
DATA_FILE="${SFROOT}/testcases/Network_Service/config.xml"
[ -e $DATA_FILE ] || { echo "$DATA_FILE not find, test end."; exit 1; }

XmlParse ${DATA_FILE}

GetValue REHOST ReHost
GetValue RUSERNAME UserName
GetValue PASSWORD RootPasswd

export RHOST=${REHOST}
export RUSERNAME=${RUSERNAME}
export PASSWORD=${PASSWORD}

export LANG=C
setup_env || { echo "setup env failed, test end."; exit 1; }

do_setup
do_test || { echo "testcase failed."; exit 1; }
